<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class OrderLogisticsModel extends Model {

    protected $table ="order_logistics";

    protected $pk = 'logistics_id';

    private $cacheKey="order:logistics:id:";

    private $cacheTime=5184000;


    /*
   * 获取total
   * */
    public function getTotal($where=[]){
        return Db::name('order_logistics')->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where,$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name('order_logistics')->field("logistics_id")->where($where);

        if(!empty($field) ) {
            $obj = $obj->field($field);
        }

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();

        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item['logistics_id']);
            }
            return $return;
        }
        return [];
    }



    /*
* 获取订单状态详情
* */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info =  json_decode($info,true);
        }else{
            $info = Db::name('order_logistics')->find($id);
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
        }

        //$info["logistics_status"] = $this->getLastLogisticsStatus($info["waybill_id"]);
        $info["spec_ids"] = !empty($info["spec_ids"]) ? json_decode($info["spec_ids"],true) : [];
        $info["wx_request"] = !empty($info["wx_request"]) ? json_decode($info["wx_request"],true) : [];
        $info["wx_response"] = !empty($info["wx_response"]) ? json_decode($info["wx_response"],true) : [];
        return $info;
    }



    /*
     * 获取售后订单状态
     * */
    public function getOrderLogisticsInfo($order_id){
        $where =[];
        $where[] = ["order_id","=",$order_id];
        $where[] = ["is_delete","=",0];

        return $this->getList($where);
    }

    /*
     * 检查快递单号是否使用过
     * */
    public function isDeliveryIdHaseUsed($order_id,$waybill_id){
        $where[] = ["order_id","=",$order_id];
        $where[] = ["waybill_id","=",$waybill_id];
        $where[] = ["is_delete","=",0];
        return $this->getTotal($where);
    }

    /*
     * 添加物流
     * */
    public function addOne($data){
        return Db::name('order_logistics')->insertGetId($data);
    }

    /*
     * 修改运单号
     * */
    public function updateOne($id, $data){
        $where[] = ["logistics_id","=",$id];
        return Db::name('order_logistics')->where($where)->update($data);
    }


    /*
     * 获取运单最新的轨迹状态
     * */
    public function getLastLogisticsStatus($waybill_id){
        $where[] = ["waybill_id","=",$waybill_id];
        return Db::name('order_logistics')->where($where)->value("logistics_status");
    }


    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }


    public function delAllCache()
    {
        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }


}